<template>
  <div class="AddressManagement">
    <navBar :title="title"></navBar>
    <div class="myAddressList">
      <ul>
        <li class="myAddressList_item" v-for="(item, index) in myAddressList" :key="index">
          <div class="myAddressList_item_left" @click="chooseAddress(item)">
            <div class="name_tel_type">
              <span class="name">{{item.consignee}}</span>
              <span class="tel">{{item.mobile}}</span>
              <!--<span class="type">{{item.is_default}}</span>-->
            </div>
            <div class="address">{{item.address}}</div>
          </div>
          <div class="myAddressList_item_right"><img width="100%" src="/img/49.png" alt='' @click="goCreateAddress(item.address_id)" /></div>
        </li>
      </ul>
    </div>
    <div class="create_address" @click="goCreateAddress()">新建收货地址</div>
  </div>
</template>

<script>
import navBar from "components/navBar";
import { mapActions } from "vuex";
export default {
  name: "AddressManagement",
  data() {
    return {
      title: "地址管理",
      myAddressList: [
      ]
    };
  },
  computed:{
    from(){
      return this.$route.query.from || undefined
    }
  },
  components: {
    navBar
  },
  mounted(){
    this.getList();
  },
  methods: {
    ...mapActions(["Address"]),
    getList(){
      this.Address().then(res=>{
        console.log('[Address]', res);
        if(res.data.status == 1){
          this.myAddressList = res.data.result
          if(this.myAddressList.length == 0 ){
            this.$cookie.del('default_address')
          }
        }
      }).catch(res=>{
        console.log('[ERROR]', res, arguments);
        this.$toast('网络错误！请联系管理员')
      })
    },
    goCreateAddress(address_id) {
        this.$router.push({name:"createAddress", query:{address_id}})
    },
    chooseAddress(item){
      if(!this.from) return false;
      this.$store.commit('setDefaultAddress', item)
      this.$router.push({name: this.from, query: this.$route.query})
    }
  }
};
</script>

<style lang="less" rel="stylesheet/less" scoped >
.myAddressList {
  padding-left: 23px;
  .myAddressList_item {
    height: 82px;
    border-bottom: 1px solid #dbdbdb;
    &:last-child {
      border-bottom: 1px solid transparent;
    }
    padding: 10px 0 20px 0;
    box-sizing: border-box;
    .myAddressList_item_left {
      float: left;
      width: 80%;
      .name_tel_type {
        span {
          display: inline-block;
        }
        .name {
          font-size: 15px;
          font-family: PingFangSC-Semibold;
          font-weight: 600;
          color: rgba(48, 48, 48, 1);
          margin-right: 41px;
        }
        .tel {
          margin-right: 6px;
          font-size: 11px;
          font-family: PingFangSC-Semibold;
          font-weight: 600;
          color: rgba(48, 48, 48, 1);
        }
        .type {
          text-align: center;
          width: 31px;
          height: 14px;
          line-height: 14px;
          background: linear-gradient(
            137deg,
            rgba(1, 47, 64, 1) 0%,
            rgba(1, 47, 64, 1) 100%
          );
          border-radius: 0px 7px 0px 7px;
          font-size: 9px;
          font-family: PingFangSC-Regular;
          font-weight: 400;
          color: rgba(255, 255, 255, 1);
        }
      }
      .address {
        margin-top: 4px;
        font-size: 11px;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: rgba(153, 153, 153, 1);
      }
    }
    .myAddressList_item_right {
      float: right;
      width: 24px;
      height: 24px;
      margin-right: 31px;
      margin-top: 20px;
    }
  }
}
.create_address {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  line-height: 41px;
  width: 333px;
  height: 41px;
  background: linear-gradient(
    90deg,
    rgba(1, 47, 64, 1) 0%,
    rgba(1, 47, 64, 1) 100%
  );
  font-size: 15px;
  font-family: PingFangSC-Regular;
  font-weight: 400;
  color: rgba(255, 255, 255, 1);
}
</style>
